//
//  PostTagViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/5.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "PostTagViewController.h"
#import "TTGTextTagCollectionView.h"
#import "UITextViewPlaceholder.h"
#import "RentDetailViewController.h"
#import "UINavigationController+HelpMe.h"
#import "MyRentListViewController.h"

@interface PostTagViewController ()<TTGTextTagCollectionViewDelegate>

@property (nonatomic, strong) TTGTextTagCollectionView *tagView1;
@property (nonatomic, strong) TTGTextTagCollectionView *tagView2;
@property (nonatomic, strong) TTGTextTagCollectionView *tagView3;


@property (nonatomic, strong) UITextViewPlaceholder *textView;

@property (nonatomic, copy) NSString *mainImageUrl;
@property (nonatomic, copy) NSString *subImageUrl;

@end

@implementation PostTagViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"其他信息";
    self.view.backgroundColor = [UIColor whiteColor];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT-SafeAreaTopHeight)];
    [self.view addSubview:scrollView];
    
    
    scrollView.contentSize = CGSizeMake(DEVICE_SCREEN_WIDTH, 1000);
    CGFloat height = 50;
    //房屋配置
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 100, 50)];
        label.text = @"房屋配置";
        label.textColor = BLACKTEXTCOLOR;
        label.font = [UIFont systemFontOfSize:15];
        [scrollView addSubview:label];
        
        self.tagView1 = [[TTGTextTagCollectionView alloc] initWithFrame:CGRectMake(12, 50, DEVICE_SCREEN_WIDTH, 50)];
        [scrollView addSubview:self.tagView1];
        self.tagView1.delegate = self;
        NSArray *arr = @[@"冰箱", @"电视", @"洗衣机", @"热水器", @"空调", @"宽带", @"沙发", @"床", @"暖气", @"衣柜", @"可做饭", @"卫生间", @"阳台", @"智能门锁", @"油烟机", @"燃气灶"];
        
        TTGTextTagConfig *config = [[TTGTextTagConfig alloc] init];
        config.textColor = BLACKTEXTCOLOR;
        config.shadowOpacity = 0;
        config.cornerRadius = 0;
        config.selectedCornerRadius = 0;
        config.extraSpace = CGSizeMake(24, 18);
        config.backgroundColor = [UIColor colorWithHexString:@"#f6f6f6" andAlpha:1];
        config.textFont = [UIFont systemFontOfSize:13];
        config.selectedTextColor = [UIColor whiteColor];
        config.selectedBackgroundColor = THEMECOLOR;
        [self.tagView1 addTags:arr withConfig:config];
        height+= self.tagView1.contentSize.height+10;
        self.tagView1.frame = CGRectMake(12, self.tagView1.y, DEVICE_SCREEN_WIDTH, self.tagView1.contentSize.height);
    }
    //房屋亮点
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, height, 100, 50)];
        label.text = @"房屋亮点";
        label.textColor = BLACKTEXTCOLOR;
        label.font = [UIFont systemFontOfSize:15];
        [scrollView addSubview:label];
        height += 50;
        self.tagView2 = [[TTGTextTagCollectionView alloc] initWithFrame:CGRectMake(12, height, DEVICE_SCREEN_WIDTH, 50)];
        [scrollView addSubview:self.tagView2];
        self.tagView2.delegate = self;
        NSArray *arr = @[@"南北通透", @"独立阳台", @"首次出租", @"可短租"];
        
        TTGTextTagConfig *config = [[TTGTextTagConfig alloc] init];
        config.textColor = BLACKTEXTCOLOR;
        config.shadowOpacity = 0;
        config.cornerRadius = 0;
        config.selectedCornerRadius = 0;
        config.extraSpace = CGSizeMake(24, 18);
        config.backgroundColor = [UIColor colorWithHexString:@"#f6f6f6" andAlpha:1];
        config.textFont = [UIFont systemFontOfSize:13];
        config.selectedTextColor = [UIColor whiteColor];
        config.selectedBackgroundColor = THEMECOLOR;
        
        [self.tagView2 addTags:arr withConfig:config];
        height+= self.tagView2.contentSize.height+10;
        self.tagView2.frame = CGRectMake(12, self.tagView2.y, DEVICE_SCREEN_WIDTH, self.tagView2.contentSize.height);
    }
    //出租要求
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, height, 100, 50)];
        
        label.text = @"出租要求";
        label.textColor = BLACKTEXTCOLOR;
        label.font = [UIFont systemFontOfSize:15];
        [scrollView addSubview:label];
        
        height += 50;
        
        self.tagView3 = [[TTGTextTagCollectionView alloc] initWithFrame:CGRectMake(12, height, DEVICE_SCREEN_WIDTH, 50)];
        [scrollView addSubview:self.tagView3];
        self.tagView3.delegate = self;
        NSArray *arr = @[@"只限女生", @"一家人", @"禁止养宠物", @"半年起租", @"一年起租", @"租户稳定", @"作息正常", @"禁止吸烟"];
        TTGTextTagConfig *config = [[TTGTextTagConfig alloc] init];
        config.textColor = BLACKTEXTCOLOR;
        config.shadowOpacity = 0;
        config.cornerRadius = 0;
        config.selectedCornerRadius = 0;
        config.extraSpace = CGSizeMake(24, 18);
        config.backgroundColor = [UIColor colorWithHexString:@"#f6f6f6" andAlpha:1];
        config.textFont = [UIFont systemFontOfSize:13];
        config.selectedTextColor = [UIColor whiteColor];
        config.selectedBackgroundColor = THEMECOLOR;
        
        [self.tagView3 addTags:arr withConfig:config];
        height+= self.tagView3.contentSize.height+10;
        self.tagView3.frame = CGRectMake(12, self.tagView3.y, DEVICE_SCREEN_WIDTH, self.tagView3.contentSize.height);
    }
    {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, height, 100, 50)];

        label.text = @"补充几句";
        label.textColor = BLACKTEXTCOLOR;
        label.font = [UIFont systemFontOfSize:15];
        [scrollView addSubview:label];

        height += 50;
        
    }
    _textView = [[UITextViewPlaceholder alloc] initWithFrame:CGRectMake(12, height, DEVICE_SCREEN_WIDTH-24, 240)];
    [scrollView addSubview:_textView];
    _textView.contentInset = UIEdgeInsetsMake(10, 10, -10, 10);
    _textView.layer.borderWidth = 1;
    _textView.layer.borderColor = SEPARATECOLOR.CGColor;
    _textView.placeholder = @"详细的描述会大大增加快速出租的机会！可以介绍房子吸引人的地方，交通和周边环境，可以入住的时间，及对租客的要求等。";
    _textView.placeholderTextColor = GRAYTEXTCOLOR;
    _textView.textColor = BLACKTEXTCOLOR;
    _textView.font = [UIFont systemFontOfSize:14];
    
    height += 260;
    
    UIButton *submitBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, height, DEVICE_SCREEN_WIDTH, 44)];
    [scrollView addSubview:submitBtn];
    submitBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    submitBtn.backgroundColor = THEMECOLOR;
    [submitBtn setTitle:@"发布转租" forState:UIControlStateNormal];
    [submitBtn addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    height += 44;
    
    scrollView.contentSize = CGSizeMake(DEVICE_SCREEN_WIDTH, height);
}
#pragma mark - 提交
-(void)submit{
    
    NSMutableArray *tagArr = [NSMutableArray array];
    [tagArr addObjectsFromArray:_tagView1.allSelectedTags];
    [tagArr addObjectsFromArray:_tagView2.allSelectedTags];
    [tagArr addObjectsFromArray:_tagView3.allSelectedTags];
    if (tagArr.count == 0) {
        [MBProgressHUD showWarning:@"请选择一些标签" toView:nil];
        return;
    }
    _mainImageUrl = @"";
    _subImageUrl = @"";
    
    
    [self uploadMainImage];
    [MBProgressHUD showMessag:@"" toView:self.view];
    
}

-(void)uploadMainImage{
    NSLog(@"上传图片%@", _mainImage);
    [NetWork uploadImageWithImage:_mainImage success:^(id responseBody) {
        self.mainImageUrl = responseBody[@"img_url"];
        NSLog(@"主图地址：%@", self.mainImageUrl);
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//            
//        });
        [self uploadSubImage];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

-(void)uploadSubImage{
    dispatch_group_t serviceGroup = dispatch_group_create();
    NSMutableArray *imageNameArr  =[NSMutableArray array];
    for(int i = 0; i< self.imageArr.count; i++){
        [imageNameArr addObject:@""];
        UIImage *image = self.imageArr[i];
        dispatch_group_enter(serviceGroup);
        NSLog(@"上传图片%@", image);
        
        [NetWork uploadImageWithImage:image success:^(id responseBody) {
            NSString *fileName = responseBody[@"img_url"];
            NSLog(@"子图地址：%@", fileName);
            [imageNameArr replaceObjectAtIndex:i withObject:fileName];
            dispatch_group_leave(serviceGroup);
        } failure:^(NSError *error) {
            dispatch_group_leave(serviceGroup);
        }];
        
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(i * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//        });
        
    }

    dispatch_group_notify(serviceGroup, dispatch_get_main_queue(), ^{
        if (imageNameArr.count == 0) {
            self.subImageUrl = @"";
        }else{
            self.subImageUrl = [imageNameArr componentsJoinedByString:@","];
        }
        
        [self finalSubmit];
    });
}


-(void)finalSubmit{
    NSMutableArray *tagArr = [NSMutableArray array];
    [tagArr addObjectsFromArray:_tagView1.allSelectedTags];
    [tagArr addObjectsFromArray:_tagView2.allSelectedTags];
    [tagArr addObjectsFromArray:_tagView3.allSelectedTags];
    
    NSDictionary *dict = @{
        @"uid": [User sharedUser].userId,
        @"token": [User sharedUser].token,
        @"lm": @"23",
        @"title": _postTitle,
        @"price": _money,
        @"city": _city,
        @"adr":  _district,
        @"xq": _communityName,
        @"hx": _roomType,
        @"lc": _floor,
        @"mj": _area,
        @"dt": _metro,
        @"fs": @"月付",
        
        @"phe": _phone,
        @"tags": [tagArr componentsJoinedByString:@","],
        @"cover": _mainImageUrl,
        @"pics": _subImageUrl,
        @"con": _textView.text,
        @"phe": _phone,
        @"lon": _lon,
        @"lat": _lat
    };
    NSLog(@"发布参数：%@", dict);
    [NetWork sendGetUrl:@"fy/fy_add.php" withParams:dict success:^(id responseBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@", responseBody);
        MyRentListViewController *vc = [MyRentListViewController new];
        [self.navigationController pushViewController:vc animated:YES];
        
        NSMutableArray *arr = [NSMutableArray arrayWithArray:[self.navigationController viewControllers]];
        [arr removeObjectAtIndex:arr.count - 2];
        [arr removeObjectAtIndex:arr.count - 2];
        self.navigationController.viewControllers = arr;
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
@end
